#pragma once
#include "Socket.hpp"
#include <sys/poll.h>
using namespace MySocket;

class PollServer
{
    const static int size = sizeof(fd_set) * 8;
    const int defaultfd = -1;

public:
    PollServer(int port) : _is_running(false)
    {
        _sockfd->BuildSocket(port);
        for (int i = 0; i < size; i++)
        {
            _fds[i].fd = defaultfd;
            _fds[i].events = 0;
            _fds[i].revents = 0;
        }
        _fds[0].fd = _sockfd->Fd();
    }

    void Start()
    {
        _is_running = true;
        int timeout = -1;
        while (_is_running)
        {
            // int maxfd = -1;
            // // 设置rfds
            // fd_set rfds;
            // FD_ZERO(&rfds);

            int n = poll(_fds, size, timeout);
            switch (n)
            {
            case -1:
                LOG(Loglevel::ERROR) << "select error...";
                break;
            case 0:
                LOG(Loglevel::ERROR) << "time out...";
                break;
            default:
                Dispatcher();
                break;
            }
        }
    }
    // 事件派发器
    void Dispatcher()
    {
        for (int i = 0; i < size; i++)
        {
            if (_fds[i].fd == defaultfd)
                continue;

            if (_fds[i].revents & POLLIN)
            {
                if (_fds[i].fd == _sockfd->Fd())
                {
                    // 网络套接字就绪，有新链接到来
                    Accepter();
                }
                else
                {
                    // 普通读事件就绪
                    Recver(i);
                }
            }
        }
    }
    void Accepter()
    {
        InetAddr client;
        int sockfd = _sockfd->Accept(&client);

        int pos = 0;
        // 找一个空位
        for (; pos < size; pos++)
            if (_fds[pos].fd == defaultfd)
                break;

        // 文件描述符满了
        if (pos == size)
        {
            LOG(Loglevel::WARN) << "select server full";
            close(sockfd);
        }
        else
        {
            _fds[pos].fd = sockfd;
            _fds[pos].events = POLLIN;
            _fds[pos].revents = 0;
        }
    }
    void Recver(int pos)
    {
        char buffer[1024];
        ssize_t n = recv(_fds[pos].fd, buffer, sizeof(buffer) - 1, 0);
        if (n > 0)
        {
            buffer[n] = 0;
            std::cout << "client say: " << buffer << std::endl;
        }
        else if (n == 0)
        {
            LOG(Loglevel::INFO) << "client quit...";
            close(_fds[pos].fd);
            _fds[pos].fd = defaultfd;
            _fds[pos].events = 0;
            _fds[pos].revents = 0;
        }
        else
        {
            LOG(Loglevel::ERROR) << "read error...";
            close(_fds[pos].fd);
            _fds[pos].fd = defaultfd;
            _fds[pos].events = 0;
            _fds[pos].revents = 0;
        }
    }

    void Stop()
    {
        _is_running = false;
    }

private:
    bool _is_running;
    std::unique_ptr<TcpSocket> _sockfd;

    struct pollfd _fds[size];
    // int _fd_array[size];
};